/**
 * Generate a fully valid Excel file for V2 menu import.
 * Output: backend/tmp/menu_v2_valid.xlsx
 */
const fs = require('fs');
const fsp = require('fs').promises;
const path = require('path');
const XLSX = require('xlsx');
const { SCHEMA } = require('../src/constants/importV2Schema');

(async () => {
  try {
    const outDir = path.join(__dirname, '../tmp');
    await fsp.mkdir(outDir, { recursive: true });
    const outPath = path.join(outDir, 'menu_v2_valid.xlsx');

    const toRowArray = (headers, obj) => headers.map(h => (obj[h] ?? ''));

    const dishesHeaders = SCHEMA.Dishes;
    const variantsHeaders = SCHEMA.Variants;
    const optionsHeaders = SCHEMA.Options;

    const dishesRows = [
      {
        dish_sku: 'SKU-VALID-001',
        dish_slug: 'valid-sku-001',
        name_zh: '有效菜品一',
        name_en: 'Valid Dish One',
        category: 'AutoTestCat',
        base_status: 'active',
        description_zh: '描述1',
        description_en: 'desc1',
        ingredients_zh: '配料1',
        ingredients_en: 'ing1',
        tags: 'tag1,tag2',
        popularity: '80'
      },
      {
        dish_sku: 'SKU-VALID-002',
        dish_slug: 'valid-sku-002',
        name_zh: '有效菜品二',
        name_en: 'Valid Dish Two',
        category: 'AnotherCat',
        base_status: 'inactive',
        description_zh: '描述2',
        description_en: 'desc2',
        ingredients_zh: '配料2',
        ingredients_en: 'ing2',
        tags: 'tag3',
        popularity: '50'
      }
    ];

    const variantsRows = [
      {
        dish_sku: 'SKU-VALID-001',
        name_zh: '标准',
        variant_code: 'STD-001',
        variant_name: 'Standard',
        price: '12.5',
        compare_at_price: '',
        is_default: 'true',
        stock: '100'
      },
      {
        dish_sku: 'SKU-VALID-002',
        name_zh: '标准',
        variant_code: 'STD-002',
        variant_name: 'Standard',
        price: '9.9',
        compare_at_price: '',
        is_default: 'true',
        stock: '50'
      }
    ];

    const optionsRows = [
      {
        dish_sku: 'SKU-VALID-001',
        name_zh: '',
        group_code: 'SPICE',
        group_name: 'Spice',
        required: 'false',
        value_code: 'MILD',
        value_name: 'Mild',
        price_delta: '0'
      },
      {
        dish_sku: 'SKU-VALID-002',
        name_zh: '',
        group_code: 'SAUCE',
        group_name: 'Sauce',
        required: 'false',
        value_code: 'SOY',
        value_name: 'Soy Sauce',
        price_delta: '0'
      }
    ];

    const wb = XLSX.utils.book_new();
    const wsDishes = XLSX.utils.aoa_to_sheet([dishesHeaders, ...dishesRows.map(r => toRowArray(dishesHeaders, r))]);
    const wsVariants = XLSX.utils.aoa_to_sheet([variantsHeaders, ...variantsRows.map(r => toRowArray(variantsHeaders, r))]);
    const wsOptions = XLSX.utils.aoa_to_sheet([optionsHeaders, ...optionsRows.map(r => toRowArray(optionsHeaders, r))]);

    XLSX.utils.book_append_sheet(wb, wsDishes, 'Dishes');
    XLSX.utils.book_append_sheet(wb, wsVariants, 'Variants');
    XLSX.utils.book_append_sheet(wb, wsOptions, 'Options');

    XLSX.writeFile(wb, outPath);
    console.log(`Generated valid file: ${outPath}`);
  } catch (err) {
    console.error('Failed to generate valid Excel:', err);
    process.exit(1);
  }
})();